package othello.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import othello.logic.Coordinates;
import othello.logic.GameGrid;
import othello.logic.MoveQueue;

/**
 * Class GameBoard
 * This class is the graphical representation of the GameGrid.
 * It paints the game field and the discs.
 */

public class GameBoard extends JLabel implements MouseListener{
    //GameGrid dimensions.
    private final int HEIGHT = 8;
    private final int WIDTH = 8;
    
    //Constans for player color.
    private final int EMPTY = 0;
    private final int BLACK = 1;
    private final int WHITE = 2;
    
    private int gameGrid[][];
    
    private MoveQueue moveQueue;
    
    public GameBoard(GameGrid gameGrid){
        this.gameGrid = gameGrid.getGrid();
        this.addMouseListener(this);
    }
    
    //Paints the board.
    @Override
    public void paint(Graphics g) {
        Dimension size = getSize();
        
        //Outer lines.
        //Vertical lines.
        g.drawLine(1, 1, 1, size.height-1);
        g.drawLine(0, 0, 0, size.height-1);
        g.drawLine(size.width-1, 1, size.width-1, size.height-1);
        
        //Horizontal lines.
        g.drawLine(0, 0, size.width, 0);
        g.drawLine(0, size.height-2, size.width, size.height-2);
        g.drawLine(0, size.height-1, size.width, size.height-1);
        
        //Inner lines.
        //Horizontal lines.
        for (int i = 1; i < HEIGHT; i++) {
            g.drawLine(0, ((size.height/8)*i), size.width, ((size.height/8)*i)); 
        }
        
        //Vertical lines.
        for (int i = 1; i < WIDTH; i++) {
            g.drawLine(((size.width/8)*i), size.height, ((size.width/8)*i), 0); 
        }  
        
        for (int i = 0; i < WIDTH; i++) {
            for (int j = 0; j < HEIGHT; j++) {
                switch (gameGrid[i][j]) {
                    case BLACK:
                        g.setColor(Color.black);
                        g.fillOval(((size.width/8)*i)+8, ((size.height/8)*j)+5, 45, 45);
                        break;
                    case WHITE:
                        g.setColor(Color.white);
                        g.fillOval(((size.width/8)*i)+8, ((size.height/8)*j)+5, 45, 45);
                        break;
                    case EMPTY:
                        break;
                }
            }
        }
    }

    //What to do when mouse is clicked.
    public void mouseClicked(MouseEvent me) {
        moveQueue.setNextMove(new Coordinates((me.getX()/60), (me.getY()/55)));
    }

    public void mousePressed(MouseEvent me){}

    public void mouseReleased(MouseEvent me){}

    public void mouseEntered(MouseEvent me){}

    public void mouseExited(MouseEvent me){}

    public void setNewGrid(GameGrid gameGrid){
        this.gameGrid = gameGrid.getGrid();
    }

    public void setNewMoveQueue(MoveQueue mq) {
        moveQueue = mq;
    }
}
